﻿Imports System
Imports System.IO
Public Class frmLogin
    Private sLoad As Boolean


    Private Sub frmLogin_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        PrPath = Directory.GetParent(Application.ExecutablePath).ToString & "\"
        txtPassword.Focus()
        txtPassword.SelectAll()
        txtPassword.Focus()
    End Sub


    Public Sub User_fill()

        On Error GoTo err_

        If DATAB = False Then
            Call LoadDatabase()
            Exit Sub
        Else
        End If

        cmbUser.Items.Clear()

        Dim rs As ADODB.Recordset

        Dim zcn As Integer
        Dim sSQL As String

        sSQL = "select count(*) as t_n from T_User"
        rs = New ADODB.Recordset
        rs.Open(sSQL, DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

        With rs
            zcn = .Fields("t_n").Value

        End With
        rs.Close()
        rs = Nothing

        If zcn = 0 Then
            sSQL = "select * from T_User"
            rs = New ADODB.Recordset
            rs.Open(sSQL, DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

            With rs
                .AddNew()
                .Fields("User_ID").Value = "ADMINISTRATOR"
                .Fields("Password").Value = "SWY\X"
                .Fields("Name").Value = "ADMINISTRATOR"
                .Fields("Level").Value = "Admin"

                .Update()
            End With
            rs.Close()
            rs = Nothing

        End If


        rs = New ADODB.Recordset
        sSQL = "select * from T_User WHERE Name <> ' '  ORDER BY Name"

        rs.Open(sSQL, DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

        With rs
            .MoveFirst()
            Do While Not .EOF

                cmbUser.Items.Add(.Fields("Name").Value)

                .MoveNext()
                'DoEvents
            Loop
        End With
        rs.Close()
        rs = Nothing


        rs = New ADODB.Recordset
        rs.Open("UPDATE spr_other SET C='0' WHERE C=''", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
        'rs.Close()
        rs = Nothing


        Exit Sub

err_:

        MsgBox(Err.Description)
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        User_fill()

        If Len(txtPassword.Text) = 0 Then Exit Sub
        
        Me.Enabled = False
        Call User_Pro()
        

        Me.Enabled = True

        'Call User_Pro()


    End Sub

    Private Sub User_Pro()
        Try
            Dim objIniFile As New IniFile(PrPath & "base.ini")
            objIniFile.WriteString("general", "DefaultUser", cmbUser.Text)

            Call LoadDatabase()

            Dim rscount As ADODB.Recordset
            Dim QWERT As Long
            rscount = New ADODB.Recordset
            rscount.Open("SELECT COUNT(*) AS total_number FROM CONFIGURE", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)


            With rscount
                QWERT = .Fields("total_number").Value
            End With

            rscount.Close()
            rscount = Nothing

            If QWERT <= 0 Then

                Dim rs25 As ADODB.Recordset
                rs25 = New ADODB.Recordset
                rs25.Open("SELECT * FROM CONFIGURE", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

                With rs25
                    .AddNew()
                    .Fields("ORG").Value = "BKO.SHATKI.INFO"
                    .Fields("SISADM").Value = "SISADM"
                    .Fields("Name_Prog").Value = "BKO.NET"
                    .Fields("Nr").Value = "Yes"
                    .Fields("access").Value = "1.7.2"
                    .Update()
                End With
                rs25.Close()
                rs25 = Nothing

            End If



            Dim rs As ADODB.Recordset
            rs = New ADODB.Recordset

            Dim sVER, tVER As String
            Dim sPass1 As String = ""


            Dim LNGIniFile As New IniFile(sLANGPATH)

            rs = New ADODB.Recordset
            rs.Open("select * from CONFIGURE", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

            With rs

                sVER = .Fields("access").Value

            End With
            rs.Close()
            rs = Nothing

            tVER = My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Build

            If sVER >= "1.7.2" Then


            Else
                MsgBox(LNGIniFile.GetString("frmLogin", "MSG1", "") & vbCrLf & LNGIniFile.GetString("frmLogin", "MSG2", ""), MsgBoxStyle.Information, "BKO.NET - " & tVER)

                End
            End If


            'langini.GetString("messages", "l1", "")

            If Len(txtPassword.Text) = 0 Then Exit Sub

            strPassword = Trim(txtPassword.Text)
            Call EncryptDecrypt(strPassword)
            txtPassword.Text = Temp





            Dim sCOUNT As Integer
            Dim T_User As ADODB.Recordset

            T_User = New ADODB.Recordset
            T_User.Open("SELECT count(*) as t_n FROM T_User where Name ='" & cmbUser.Text & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

            With T_User
                sCOUNT = .Fields("t_n").Value
            End With
            T_User.Close()
            T_User = Nothing



            If sCOUNT = 0 Then
                MsgBox("This User is not valid", MsgBoxStyle.Critical, "Error!!!")
                Exit Sub
            End If


            T_User = New ADODB.Recordset
            T_User.Open("SELECT * FROM T_User where Name ='" & cmbUser.Text & "'", DB7, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

            With T_User
                .MoveFirst()
                Do While Not .EOF

                    If cmbUser.Text = .Fields("Name").Value Then

                        If cmbUser.Text = .Fields("Name").Value And txtPassword.Text = .Fields("password").Value Then


                            uLevel = .Fields("Level").Value
                            uSERID = .Fields("User_ID").Value
                            UserNames = .Fields("Name").Value

                            If uLevel = "Admin" Then

                            Else



                                If .Fields("SETUP").Value = 1 Or .Fields("SETUP").Value = True Then
                                    uLevelSetup = True
                                Else
                                    uLevelSetup = False
                                End If
                                If .Fields("PCADD").Value = 1 Or .Fields("PCADD").Value = True Then
                                    uLevelTehAdd = True
                                Else
                                    uLevelTehAdd = False
                                End If
                                If .Fields("PCDEL").Value = 1 Or .Fields("PCDEL").Value = True Then
                                    uLevelTehDel = True
                                Else
                                    uLevelTehDel = False
                                End If
                                If .Fields("CAPADD").Value = 1 Or .Fields("CAPADD").Value = True Then
                                    uLevelNotesAdd = True
                                Else
                                    uLevelNotesAdd = False
                                End If

                                If .Fields("CAPDEL").Value = 1 Or .Fields("CAPDEL").Value = True Then
                                    uLevelNotesDel = True
                                Else
                                    uLevelNotesDel = False
                                End If
                                If .Fields("REPADD").Value = 1 Or .Fields("REPADD").Value = True Then
                                    uLevelRepAdd = True
                                Else
                                    uLevelRepAdd = False
                                End If

                                If .Fields("REPDEL").Value = 1 Or .Fields("REPDEL").Value = True Then
                                    uLevelRepDel = True
                                Else
                                    uLevelRepDel = False
                                End If
                                If .Fields("REPed").Value = 1 Or .Fields("REPed").Value = True Then
                                    uLevelRepEd = True
                                Else
                                    uLevelRepEd = False
                                End If

                                If .Fields("POADD").Value = 1 Or .Fields("POADD").Value = True Then
                                    uLevelPOAdd = True
                                Else
                                    uLevelPOAdd = False
                                End If
                                If .Fields("PODEL").Value = 1 Or .Fields("PODEL").Value = True Then
                                    uLevelPODel = True
                                Else
                                    uLevelPODel = False
                                End If
                                If .Fields("CARTR").Value = 1 Or .Fields("CARTR").Value = True Then
                                    uLevelCart = True
                                Else
                                    uLevelCart = False
                                End If
                                If .Fields("PO").Value = 1 Or .Fields("PO").Value = True Then
                                    uLevelPO = True
                                Else
                                    uLevelPO = False
                                End If
                                If .Fields("SCLAD").Value = 1 Or .Fields("SCLAD").Value = True Then
                                    uLevelWarehause = True
                                Else
                                    uLevelWarehause = False
                                End If



                            End If

                            Call SaveActivityToLogDB(LNGIniFile.GetString("frmLogin", "MSG3", ""))

                            Call ALTER_DB()
                            Me.Hide()
                            frmMain.Show()

                            Me.Close()

                        Else

                            Call SaveActivityToLogDB(LNGIniFile.GetString("frmLogin", "MSG4", ""))

                            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
                            Me.Enabled = True
                            MsgBox("This Password is not valid", MsgBoxStyle.Critical, "Error!!!")
                            txtPassword.Text = ""
                            txtPassword.Focus()
                            Me.Enabled = True
                        End If

                    Else
                    End If
                    .MoveNext()
                    'DoEvents
                Loop
            End With
            T_User.Close()
            T_User = Nothing

        Catch ex As Exception
            LogDBErrors()
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        DBserv = ConnectSetup.server_ip.Text
        DBtabl = ConnectSetup.db_name.Text
        DBuser = ConnectSetup.dbu_name.Text
        DBpass = ConnectSetup.db_pass.Text
        DBport = ConnectSetup.server_port.Text


        If DATAB = True Then

            UnLoadDatabase()
            Call LoadDatabase()

        Else
            Call LoadDatabase()
        End If

        Call User_fill()

    End Sub

    Private Sub txtPassword_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyDown

        If Len(txtPassword.Text) = 0 Then Exit Sub

        Select Case e.KeyCode

            Case Keys.Enter
                Me.Enabled = False
                Call User_Pro()

        End Select

    End Sub

    Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'SendFonts(Me)


        Call LANG_frmLogin()

        Dim objIniFile As New IniFile(PrPath & "base.ini")
        ConnectSetup.cmbLang.Text = objIniFile.GetString("general", "LANG", "ru.ini")

        Call ConnectSetup.Find_LANG()

        sLoad = True

        'Me.Height = 230
        'gbData.Top = 54
        'cmbBD.Enabled = True
        'btnLogin.Top = 174
        'btnCancel.Top = btnLogin.Top
        'gbsql.Top = 205





        Call Load_BD_Settings()
        'Call User_fill()

        Me.Focus()
        txtPassword.Focus()


    End Sub

    Private Sub Load_BD_Settings()

        Dim dbType As String

        If sLoad = False Then Exit Sub

        Dim SetupIniFile As New IniFile(PrPath & "base.ini")
        cmbUser.Text = SetupIniFile.GetString("general", "DefaultUser", "")
        ConnectSetup.cmbSUBD.Text = SetupIniFile.GetString("DB", "DB", "")


        If Len(ConnectSetup.cmbSUBD.Text) = 0 Then ConnectSetup.cmbSUBD.Text = "MS Access"

        'If DATAB = False Then
        'Call LoadDatabase()
        'Else
        'Call UnLoadDatabase()
        'Call LoadDatabase()
        'End If

        dbType = SetupIniFile.GetString("DB", "DB", "")
        ConnectSetup.cmbSUBD.Text = dbType
        unamDB = dbType

        Select Case dbType

            Case "MS Access"

                ConnectSetup.cmbBD.Text = SetupIniFile.GetString("general", "file", "")

                BasePath = SetupIniFile.GetString("general", "BasePath", "")

                'Call Find_DB()
            Case Else

                Me.AutoSize = True

                ConnectSetup.server_ip.Text = SetupIniFile.GetString("DB", "SERVER", "")
                ConnectSetup.server_port.Text = SetupIniFile.GetString("DB", "PORT", "")
                ConnectSetup.db_name.Text = SetupIniFile.GetString("DB", "BD", "")
                ConnectSetup.dbu_name.Text = SetupIniFile.GetString("DB", "USER", "")
                ConnectSetup.db_pass.Text = SetupIniFile.GetString("DB", "PASSWORD", "")

                DBserv = SetupIniFile.GetString("DB", "SERVER", "")
                DBtabl = SetupIniFile.GetString("DB", "PORT", "")
                DBuser = SetupIniFile.GetString("DB", "BD", "")
                DBpass = SetupIniFile.GetString("DB", "USER", "")
                DBport = SetupIniFile.GetString("DB", "PASSWORD", "")

                'SetupIniFile.WriteString("DB", "DB", ConnectSetup.cmbSUBD.Text)


        End Select



        'Call User_fill()

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles db_setup_link.LinkClicked
        ConnectSetup.Show()
        ConnectSetup.Focus()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        End
    End Sub
End Class
